package com.sptpc.learnspring.controller;

import com.sptpc.learnspring.controller.request.NewDrinkRequest;
import com.sptpc.learnspring.model.Drink;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/drink")
public class DrinkController {

    // 前端使用json提交数据
    @PostMapping(value = "/add", consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Drink addNewDrink(@RequestBody @Valid NewDrinkRequest newDrink) {
        log.info("创建新饮品{}", newDrink);
        Drink drink = new Drink();
        BeanUtils.copyProperties(newDrink, drink);
        drink.setId(2);
        log.info("创建成功");
        return drink;
    }

    @GetMapping("/{id}")
    public Drink getDrinkById(@PathVariable Integer id) {
        log.info("查询饮品，id: {}", id);
        return Drink.builder().id(3)
                .name("茉莉花茶")
                .price(Money.of(CurrencyUnit.of("CNY"), 10.00))
                .build();
    }

}
